﻿using System.Collections.Generic;
using System.Web.UI;

namespace DMS.Business.Extends
{
    /// <summary>
    /// 扩展Page类
    /// </summary>
    public static class PageExtend
    {
        /// <summary>
        /// 返回页面所有Form下的控件
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public static List<Control> AllFormControls(this Page page)
        {
            var ctlList = new List<Control>();
            var ctls = page.Form.Controls;

            foreach (Control ctl in ctls)
            {
                FindControls(ctl, ctlList);

            }
            
            return ctlList;

        }

        public static List<Control> AllFormControls(this Page page,string controlId)
        {
            var ctlList = new List<Control>();
            var ctls = page.Form.Controls;

            foreach (Control ctl in ctls)
            {

                FindControls(ctl, ctlList, controlId);

            }
            return ctlList;

        }

        /// <summary>
        /// 获取当前页面下指定的控件
        /// </summary>
        /// <param name="page"></param>
        /// <param name="controlId"></param>
        /// <returns></returns>
        public static Control FindControlExtend(this Page page ,string controlId)
        {
            Control t = null;
            var ctls = page.Form.Controls;
            foreach (Control c in ctls)
            {
                t = FindControlX(c, controlId);
                if( t != null)
                {
                    return t;
                }
            }
            return t;
            // return FindControlX(page, controlId);
        }


        #region #私有工具方法

        private static Control FindControlX(Control ctl,string controlId)
        {
            if(ctl == null || controlId == null)
            {
                return null;
            }
            if(ctl.ID == null)
            {
                int i = 1;

                if(ctl.ID == null)
                {
                    int j = 1;
                }
            }
            if (controlId.Equals(ctl.ID))
            {
                return ctl;
            }
            if(ctl.HasControls())
            {
                foreach (Control c in ctl.Controls)
                {
                    return FindControlX(c, controlId);
                }
            }
            return null;
        }

        /// <summary>
        /// 递归获取子控件
        /// </summary>
        /// <param name="ctl"></param>
        /// <param name="ctlList"></param>
        private static void FindControls(Control ctl,List<Control> ctlList)
        {
            if (ctl == null || ctlList == null)
            {
                return;
            }
            ctlList.Add(ctl);

            if (ctl.HasControls())
            {
                foreach (Control c in ctl.Controls)
                {
                    FindControls(c, ctlList);
                }
            }

        }

        private static void FindControls(Control ctl, List<Control> ctlList,string controlId)
        {
            if (ctl == null || ctlList == null)
            {
                return;
            }

            if(controlId == ctl.ID)
            {
                ctlList.Add(ctl);
            }
            

            if (ctl.HasControls())
            {
                foreach (Control c in ctl.Controls)
                {
                    FindControls(c, ctlList, controlId);
                }
            }

        }

        #endregion
    }
}
